package cn.tedu.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author Amos
 * @date 2022/5/10
 * Text: k2, 词
 * IntWritable:v2: 出现的一次
 * Text: k3, 词
 * IntWritable: v3: 一共出现的次数
 */


public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    /**
     *
     * @param key v2, 词
     * @param values v2, 按照词进行聚合(1,1,1,1)
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 将聚合后的数据累加求和hello (1,1,1,1,1) => hello 5
        int count = 0;
        for (IntWritable value : values) {
            count += value.get();
        }
        // 写出数据
        context.write(key,new IntWritable(count));
    }
}
